package net.mysoft.budgetsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.mysoft.budgetsoft.system.dao.FlowTemplateDao;
import net.mysoft.budgetsoft.system.domain.TFlowTemplate;
import net.mysoft.budgetsoft.system.pagemodel.FlowTemplate;
import net.mysoft.budgetsoft.system.service.FlowTemplateService;
import net.mysoft.framework.model.DataGrid;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**   
 *  
 * @author dagongPGP v1.0 
 * @link <a href=http://www.dagongsoft.net>北京大公信软科技有限公司</a> 
 *  
 * @version $Revision: 1.00  $Date: 2013-07-08 17:26:47 
 */
@Service("flowTemplateService")
public class FlowTemplateServiceImpl implements FlowTemplateService{
	@Resource
	private FlowTemplateDao flowTemplateDao;
	
	public FlowTemplateDao getFlowTemplateDao(){
		return this.flowTemplateDao;
	}
	
	public void setFlowTemplateDao(FlowTemplateDao flowTemplateDao){
		this.flowTemplateDao = flowTemplateDao;
	}
	
	@Override
	public void add(FlowTemplate flowTemplate){
		TFlowTemplate t = new TFlowTemplate();
		BeanUtils.copyProperties(flowTemplate, t);
		//t.setFtl_ID(UUID.randomUUID().toString());
		flowTemplateDao.save(t);
	}
	
	@Override
	public void edit(FlowTemplate flowTemplate){
		TFlowTemplate t = new TFlowTemplate();
		BeanUtils.copyProperties(flowTemplate, t);
		flowTemplateDao.update(t);
	}
	
	@Override
	public void delete(FlowTemplate flowTemplate) {
		for (String id : flowTemplate.getIds().split(",")) {
			Object t =  flowTemplateDao.get(TFlowTemplate.class, id);
			if (t != null) {
				 flowTemplateDao.delete(t);
			}
		}
	}
	
	public void publish(FlowTemplate flowTemplate){
		TFlowTemplate t =  (TFlowTemplate) flowTemplateDao.get(TFlowTemplate.class, flowTemplate.getFtl_ID());
		t.setFtl_Valid(1);
		flowTemplateDao.update(t);
	}
	public void unpublish(FlowTemplate flowTemplate){
		TFlowTemplate t =  (TFlowTemplate) flowTemplateDao.get(TFlowTemplate.class, flowTemplate.getFtl_ID());
		t.setFtl_Valid(0);
		flowTemplateDao.update(t);
	}
	
	@Override
	public DataGrid datagrid(FlowTemplate flowTemplate){
		DataGrid dg = new DataGrid();
		String hql = "from TFlowTemplate t where 1=1 ";
		List params = new ArrayList();
		hql = addWhere(flowTemplate, hql, params);
		hql = addOrder(flowTemplate, hql);
		String totalHql = "select count(*) " + hql;
		List<TFlowTemplate> toList = flowTemplateDao.find(hql, params, flowTemplate.getPage(), flowTemplate.getRows());
		List<FlowTemplate> oList = new ArrayList<FlowTemplate>();
		oList = changeModel(toList);
		dg.setTotal(flowTemplateDao.count(totalHql, params));
		dg.setRows(oList);
		return dg;
	}
	
	private String addWhere(FlowTemplate flowTemplate, String hql, List<Object> params) {
		if(flowTemplate.getFtc_ID()!=null&&!"".equals(flowTemplate.getFtc_ID())){
			hql +=" and t.ftc_ID = ?";
			params.add(flowTemplate.getFtc_ID());
		}
		if(flowTemplate.getFtlName()!=null&&!"".equals(flowTemplate.getFtlName())) {
			hql += " and (t.ftl_Name_cn like ? or ftl_Name_en like ?) ";
			params.add("%"+flowTemplate.getFtlName()+"%");
			params.add("%"+flowTemplate.getFtlName()+"%");
		}
		if(flowTemplate.getFtl_Valid()!=null) {
			hql += " and t.ftl_Valid=? ";
			params.add(flowTemplate.getFtl_Valid());
		}
		return hql;
	}
	
	private String addOrder(FlowTemplate flowTemplate, String hql) {
		if (flowTemplate.getSort() != null) {
			hql += " order by " + flowTemplate.getSort() + " " + flowTemplate.getOrder();
		}
		return hql;
	}
	
	public List<FlowTemplate> changeModel(List<TFlowTemplate> tFlowTemplate ){
		List<FlowTemplate> list = new ArrayList<FlowTemplate>();
		for(TFlowTemplate to:tFlowTemplate){
			FlowTemplate o = new FlowTemplate();
			BeanUtils.copyProperties(to, o);
			list.add(o);
		}
		return list;
	}

	@Override
	public List find(FlowTemplate flowTemplate) {
		String hql = "from TFlowTemplate t where 1=1 ";
		List<TFlowTemplate> toList = flowTemplateDao.find(hql);
		List<FlowTemplate> oList = new ArrayList<FlowTemplate>();
		oList = changeModel(toList);
		return oList;
	}
}
